const { BaseController } = require('../Base')

class Admin extends BaseController {
  constructor() {
    super()
    this.loginUserList = {}
  }
  // 登录获取token
  login(ctx) {
    const valiList = [
      { name: 'username', msg: '用户名不能为空!' },
      { name: 'password', msg: '密码不能为空!' }
    ]
    let validate = this.tools.validate(ctx.request.body, valiList)
    if (validate) {
      return (ctx.body = this.resError(validate))
    }
    const token = this.tools.jwt.jwtToken(ctx.request.body)
    const userName = ctx.request.body.username
    ctx.body = this.resSuccess({ userName, token })
  }
  // 登录获取用户信息，权限信息
  getUserInfo(ctx) {
    let token = this.tools.getToken(ctx)
    let userInfo = this.tools.jwt.jwtVerify(token).decoded
    ctx.body = this.resSuccess(userInfo)
  }
  // 注销登录，清除缓存
  userLogout(ctx) {
    ctx.body = this.resSuccess('注销登录成功')
  }
  // 用户列表
  userList(ctx) {
    const res = this.servers.MockData.userList(ctx)
    ctx.body = this.resSuccess(res)
  }
}
const ex = new Admin()
module.exports = ex
